//package com.douqu.game.merge.service;
//
//import com.bean.core.util.TimeUtils;
//import com.douqu.game.core.database.model.PlayerModel;
//import com.douqu.game.core.database.model.ServerModel;
//import com.douqu.game.core.database.model.UserModel;
//import com.douqu.game.core.factory.WorldInfoFactory;
//import com.douqu.game.core.protobuf.SGCommonProto;
//import com.douqu.game.core.util.LogUtils;
//import com.douqu.game.core.util.Utils;
//import com.douqu.game.merge.database.config.FirstDataSourceConfig;
//import com.douqu.game.merge.database.config.SecondDataSourceConfig;
//import com.douqu.game.merge.database.mapper.first.FirstPlayerMapper;
//import com.douqu.game.merge.database.mapper.first.FirstServerMapper;
//import com.douqu.game.merge.database.mapper.first.FirstUserMapper;
//import com.douqu.game.merge.database.mapper.second.SecondPlayerMapper;
//import com.douqu.game.merge.database.mapper.second.SecondServerMapper;
//import com.douqu.game.merge.database.mapper.second.SecondUserMapper;
//import com.douqu.game.merge.database.mapper.target.TargetPlayerMapper;
//import com.douqu.game.merge.database.mapper.target.TargetServerMapper;
//import com.douqu.game.merge.database.mapper.target.TargetUserMapper;
//import com.douqu.game.merge.manager.ConnectCallback;
//import com.douqu.game.merge.manager.SpringContext;
//import com.douqu.game.merge.netty.NettyTCPClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//
///**
//* @Author: Bean
//* @Description:
//* @Date: 2017-12-27 9:34
//*/
//@Service
//public class MergeService {
//
//    @Autowired
//    private FirstPlayerMapper playerMapper1;
//
//    @Autowired
//    private FirstUserMapper userMapper1;
//
//    @Autowired
//    private FirstServerMapper serverMapper1;
//
//    @Autowired
//    private SecondPlayerMapper playerMapper2;
//
//    @Autowired
//    private SecondUserMapper userMapper2;
//
//    @Autowired
//    private SecondServerMapper serverMapper2;
//
//    @Autowired
//    private TargetPlayerMapper playerMapper;
//
//    @Autowired
//    private TargetUserMapper userMapper;
//
//    @Autowired
//    private TargetServerMapper serverMapper;
//
//    @Autowired
//    FirstDataSourceConfig firstConfig;
//
//    @Autowired
//    SecondDataSourceConfig secondConfig;
//
//    public void createPlayer(int startId, int endId)
//    {
//        Date date = new Date();
//        WorldInfoFactory worldInfo = WorldInfoFactory.getInstance();
//        for(int i = startId; i < endId + 1; i++)
//        {
//            UserModel userModel = new UserModel();
//            userModel.setIp("127.0.0.1");
//            userModel.setDeviceId(Utils.createAccount(i + "", SGCommonProto.E_CHANNEL_TYPE.CHANNEL_TYPE_QUICK));
//            userModel.setChannel(SGCommonProto.E_CHANNEL_TYPE.CHANNEL_TYPE_QUICK.getNumber());
//            userModel.setCreateTime(date);
//            userModel.setIsDel(false);
//            userMapper.insert(userModel);
//
//            PlayerModel playerModel = new PlayerModel();
//            playerModel.setUid(userModel.getId());
//            playerModel.setCreateTime(date);
//            playerModel.setObjectIndex(worldInfo.createPlayerIndex());
//            playerModel.setName("TEST_" + i);
//            playerModel.setAvatar("avatar_"+i);
//            playerModel.setCamp(1);
//            playerModel.setLevel(1);
//            playerModel.setMaster(1);
//            playerModel.setFc(0);
//            playerModel.setMoney(0);
//            playerModel.setLastLoginTime(date);
//            playerModel.setIsDel(false);
//            playerMapper.insert(playerModel);
//
//            LogUtils.info("创建玩家: " + playerModel.getObjectIndex() + " 成功!");
//        }
//
//        LogUtils.info("创建测试玩家数据 " + startId + " -> " + endId + " 结束!");
//    }
//
//    public void serverIsRunning(ConnectCallback callback)
//    {
//        new NettyTCPClient().start(firstConfig.host, firstConfig.port, connect -> {
//            if(connect)
//            {
//                LogUtils.info("服务器 -> " + firstConfig.host + ":" + firstConfig.port + " 正在运行中,请关闭后再执行合区操作!");
//                callback.connect(true);
//                return;
//            }
//        });
//
//        new NettyTCPClient().start(secondConfig.host, secondConfig.port, connect -> {
//            if(connect)
//            {
//                LogUtils.info("服务器 -> " + secondConfig.host + ":" + secondConfig.port + " 正在运行中,请关闭后再执行合区操作!");
//                callback.connect(true);
//                return;
//            }
//        });
//
//        callback.connect(false);
//    }
//    public void merge()
//    {
//        serverIsRunning(connect -> {
//            if(connect)
//            {
//                SpringContext.stop();
//                System.exit(0);
//                return;
//            }
//        });
//
//        List<PlayerModel> playerList1 = playerMapper1.findAll();
//        List<UserModel> userList1 = userMapper1.findAll();
//        ServerModel serverModel1 = serverMapper1.find();
//
//        LogUtils.info("1player count -> " + playerList1.size());
//        LogUtils.info("1user count -> " + userList1.size());
//
//        Map<Integer, MergePlayer> playerMap = new HashMap<>();
//        List<MergePlayer> mergePlayerList = new ArrayList<>();
//        MergePlayer mergePlayer = null;
//        for(UserModel userModel : userList1)
//        {
//            mergePlayer = new MergePlayer();
//            mergePlayer.setUserModel(userModel);
//
//            playerMap.put(userModel.getId(), mergePlayer);
//            mergePlayerList.add(mergePlayer);
//        }
//        for(PlayerModel playerModel : playerList1)
//        {
//            mergePlayer = playerMap.get(playerModel.getUid());
//            mergePlayer.setPlayerModel(playerModel);
//        }
//
//
//        List<PlayerModel> playerList2 = playerMapper2.findAll();
//        List<UserModel> userList2 = userMapper2.findAll();
//        ServerModel serverModel2 = serverMapper2.find();
//
//        LogUtils.info("2player count -> " + playerList2.size());
//        LogUtils.info("2user count -> " + userList2.size());
//
//        playerMap.clear();
//        for(UserModel userModel : userList2)
//        {
//            mergePlayer = new MergePlayer();
//            mergePlayer.setUserModel(userModel);
//
//            playerMap.put(userModel.getId(), mergePlayer);
//            mergePlayerList.add(mergePlayer);
//        }
//        for(PlayerModel playerModel : playerList2)
//        {
//            mergePlayer = playerMap.get(playerModel.getUid());
//            mergePlayer.setPlayerModel(playerModel);
//        }
//
//        LogUtils.info("所有玩家数据 -> " + mergePlayerList.size());
//        LogUtils.info(mergePlayerList);
//
//        //TODO 合并的时候要注意,要通知登录服务器更换服务器ID
//
//        LogUtils.info("合并玩家数据开始 -> " + TimeUtils.getCurrentTime());
//        int i = 0;
//        for(MergePlayer player : mergePlayerList)
//        {
//            userMapper.insert(player.getUserModel());
//
//            player.getPlayerModel().setUid(player.getUserModel().getId());
//
//            playerMapper.insert(player.getPlayerModel());
//        }
//
//        LogUtils.info("合并玩家数据结束 -> " + TimeUtils.getCurrentTime());
//
//        LogUtils.info("合并公共数据开始 -> " + TimeUtils.getCurrentTime());
//        ServerModel serverModel = new ServerModel();
//        int min = Math.min(serverModel1.getId(), serverModel2.getId());
//        int max = Math.max(serverModel1.getId(), serverModel2.getId());
//        serverModel.setId(min*10000 + max);
//        serverModel.setPlayerIndex(100000);
//        serverMapper.insert(serverModel);
//        LogUtils.info("合并公共数据结束 -> " + TimeUtils.getCurrentTime());
//
//
//    }
//
//
//    public class MergePlayer
//    {
//        private PlayerModel playerModel;
//
//        private UserModel userModel;
//
//        public PlayerModel getPlayerModel() {
//            return playerModel;
//        }
//
//        public void setPlayerModel(PlayerModel playerModel) {
//            this.playerModel = playerModel;
//        }
//
//        public UserModel getUserModel() {
//            return userModel;
//        }
//
//        public void setUserModel(UserModel userModel) {
//            this.userModel = userModel;
//        }
//
//        @Override
//        public String toString() {
//            return "MergePlayer{" +
//                    "playerModel=" + playerModel +
//                    ", userModel=" + userModel +
//                    '}';
//        }
//    }
//
//}
